<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link       http://docs.joomla.org/Category:Development
 * @license    GNU/GPL
 */
 
// no direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');
 
/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
 
class NvModelBlogs extends JModel
{	
	function getWikiMashup( $str )
	{
		$out = '';
		$str = trim(ucwords($str));
		$count = 0;
		
		// Get The Wiki Content
		$xml = $this->getTheContent($str);
		$count = count($xml);
		
		//if( $count == 1 )
		if( preg_match('/#ALIH /', $xml[0]) )
		{
			$st = explode('#ALIH ',$xml[0]);
			$str = trim($st[1]);
			$xml = $this->getTheContent($str);
			$count = count($xml);
		}// end of if
		
		$isPrinted = false;
		
		//for Wiki Header
		$out .= '<h4>'.$str.'</h4>';
		
		//echo $count.'<br>';
		//print_r($xml).'<br>';
		
		for($i=0;$i<$count;$i++)
		{
			if(preg_match("|<[b]+>(.*)</[b]+>|U", $xml[$i]))
			{
				$isPrinted = true;
				//continue;
			}//end of if Catch FIRST BOLD STRING
			
			if($isPrinted)
			{
				if(!empty($xml[$i]))
				{
					if(preg_match("/(.*)\./", $xml[$i]))
					{
						/*$text = preg_replace('/\w+\|/', '', $xml[$i]);
						$out .= $text."<br><br>";*/
						$out .= $xml[$i]."<br><br>";
					}
						
					else
						break;
					
					$t--;
					continue;
				}
			}//end of if isPrinted
			
		}//end of for
		
		// Create Link to Wikipedia bout the object
		$objek = $this->wikiPattern($str);
		$out .= '<a href="http://id.wikipedia.org/wiki/'.$objek.'">read more..</a><br>';
		
		return $out;
		
	}//end of function getWikiMashup

	function getIndoTravel( $str )
	{
		$out = '';
		
		if($objek = str_replace(" ","+",$str)){
			//cek apakah $str ada spasinya atau tidak
		}
		else
			$objek = $str;
		
		$options = array();
		$options['rssUrl'] = 'http://pipes.yahoo.com/pipes/pipe.run?_id=01131bc577bdcd748b13048197f51d37&_render=rss&count=5&objek='.trim($objek).'&urlfeed="http://feeds2.feedburner.com/allindotravel?format=xml"';
		//$options['rssUrl'] = 'http://localhost/wiki/kompas.xml';
		$rssDoc =& JFactory::getXMLparser('RSS', $options);
		
		/**
		*	Catch the stream (RSS) from yahoo! pipes
		*	then Fetch the content.
		**/
		if ($rssDoc != false)
		{
			// channel header and link
			$out .= "<h4>See another description about \"".ucwords($str)."\" (from http://www.indonesia.travel/) :</h4>";
			
			// items
			$items = $rssDoc->get_items();
			$count = $rssDoc->get_item_quantity();
			
			// Catch if the items > 0
			if($count > 0)
			{
				foreach($items as $item)
				{
					$out .= '<b><a href="'.$item->get_permalink().'">'.$item->get_title().'</a></b><br/>';
					//catch if there are a description
					if(trim($item->get_description()) != null)
					{
						$desc = $item->get_description();
						//remove the image, if there exist
						if(preg_match("|<(.*)>|U", $desc))
							$desc =  preg_replace('|<(.*)>|U', '', $desc);
						$out .= $desc.'<br/><br/>';
					}
					
				}//end of foreach
			}
			else
				$out .= "The request did not return results (zero result).";
			
		}//end of if $rssDOc == true
		else
		{
			$out .= "<h4>Sorry, there are no description about \"".ucwords($str)."\" (from http://www.indonesia.travel/).</h4>";
			$out .= "Bad Feed/RSS URL! or Content can't be retreive!";
		}//end of else
		
		return $out;
		
	}//end of function getBlogMashup

	function getBlogMashup( $str ,$count)
	{		
		$out = '';
		
		if($objek = str_replace(" ","-",$str)){
			//cek apakah $str ada spasinya atau tidak
		}
		else
			$objek = $str;
		
		$options = array();
		$options['rssUrl'] = 'http://pipes.yahoo.com/pipes/pipe.run?_id=3bf73e99a0d85df34398fab1321f9d46&_render=rss&count='.$count.'&enfeed="http://id.wordpress.com/tag/'.$objek.'/feed/"&idfeed="http://en.wordpress.com/tag/'.$objek.'/feed/"';
		//$options['rssUrl'] = 'http://localhost/wiki/kompas.xml';
		$rssDoc =& JFactory::getXMLparser('RSS', $options);
		
		/**
		*	Catch the stream (RSS) from yahoo! pipes
		*	then Fetch the content.
		**/
		if ($rssDoc != false)
		{
			// channel header and link
			$out .= "<h4>Related Search (blog mashup) about \"".ucwords($str)."\" :</h4>";
			//echo $rssDoc->get_link()."<br/>";
			//echo $rssDoc->get_description()."<br/>";
			
			// items
			$items = $rssDoc->get_items();
			$count = $rssDoc->get_item_quantity();
			
			// Catch if the items > 0
			if($count > 0)
			{
				foreach($items as $item)
				{
					$out .= '<b><a href="'.$item->get_permalink().'" class="lightwindow page-options"  title="'.$item->get_title().'">'.$item->get_title().'</a></b><br/>';
					//catch if there are a description
					if($item->get_description() != null)
					{
						$desc = $item->get_description();
						//remove the image, if there exist
						if(preg_match("|<(.*)>|U", $desc))
							$desc =  preg_replace('|<(.*)>|U', '', $desc);
						$out .= $desc.'<br/><br/>';
					}
					
				}//end of foreach
			}
			else
				$out .= "The request did not return results (zero result).";

		}//end of if $rssDOc == true
		else
		{
			$out .= "<h4>Related Search (blog mashup) about \"".ucwords($str)."\" :</h4>";
			$out .= "Bad Feed/RSS URL! or Content can't be retreive!";
		}//end of else
		
		return $out;
		
	}//end of function getBlogMashup

	function getNewsMashup( $str )
	{
		$out = '';
		
		if($objek = str_replace(" ","+",$str)){
			//cek apakah $str ada spasinya atau tidak
		}
		else
			$objek = $str;
		
		$options = array();
		$options['rssUrl'] = 'http://pipes.yahoo.com/pipes/pipe.run?_id=69cdeb4c9b809b67e67e1942e7fa2a95&_render=rss&count=5&objectname='.trim($objek).'&urlfeed="http://www.kompas.com/getrss/travel/"';
		//$options['rssUrl'] = 'http://localhost/wiki/kompas.xml';
		$rssDoc =& JFactory::getXMLparser('RSS', $options);
		
		/**
		*	Catch the stream (RSS) from yahoo! pipes
		*	then Fetch the content.
		**/
		if ($rssDoc != false)
		{
			// channel header and link
			$out .= "<h4>Related Search (news mashup) about \"".ucwords($str)."\" :</h4>";
			
			// items
			$items = $rssDoc->get_items();
			$count = $rssDoc->get_item_quantity();
			
			// Catch if the items > 0
			if($count > 0)
			{
				foreach($items as $item)
				{
					$out .= '<b><a href="'.$item->get_permalink().'">'.$item->get_title().'</a></b><br/>';
					//catch if there are a description
					if($item->get_description() != null)
					{
						$desc = $item->get_description();
						//remove the image, if there exist
						if(preg_match("|<(.*)>|U", $desc))
							$desc =  preg_replace('|<(.*)>|U', '', $desc);
						$out .= $desc.'<br/><br/>';
					}
					
				}//end of foreach
			}
			else
				$out .= "The request did not return results (zero result).";

		}//end of if $rssDOc == true
		else
		{
			$out .= "<h4>Related Search (news mashup) about \"".ucwords($str)."\" :</h4>";
			$out .= "Bad Feed/RSS URL! or Content can't be retreive!";
		}//end of else
		
		return $out;
		
	}//end of function getBlogMashup

	function getNewsMashup_backup( $rows, $str )
	{
		$out = '';
		
		foreach($rows as $row)
		{
			$out .= "<h4>$row->url</h4><br/>";
		}//end of foreach
		
		return $out;
		
	}//end of function getNewsMashup

	function getTheContent($str)
	{
		$objek = $this->wikiPattern($str);

		$lang = 'id';
		
		$xml = $this->fetch("http://".$lang.".wikipedia.org/w/api.php?action=query&prop=revisions&titles=".$objek."&rvprop=content&format=xml","<rev xml:space=\"preserve\">","</rev>");
		//$xml = $this->fetch("http://localhost/wiki/sempu.xml","<rev>","</rev>");
		
		$xml = $this->doHeadings($xml);
		$xml = $this->doAllQuotes($xml);
		$xml = str_replace('[[','',$xml); /* WIKIMEDIA IDENTIFY THE CONTENT BETWEEN [[ and ]] AS A LINK AND I REMOVE IT. */
		$xml = str_replace(']]','',$xml);

		$i = 0;
		$temp = $xml;
		$xml = explode("\n",$temp);
		
		return $xml;
		
	}//end of the Content

	function wikiPattern($str)
	{
		if($objek = str_replace(" ","_",$str)){
			// Cek Apakah STRING ada spasi atau tidak
		}
		else
			$objek = $str;
			
		return $objek;
	}//end of wiki pattern

	function fetch($url,$start,$end)
	{
		if($page = file_get_contents($url))
		{
			if($s1=explode($start, $page) && $s2=explode($end, $page))
			{
			 	$cont =  preg_replace('|<rev (.*)>|U', '', $page);
				//$s1=explode($start, $page);
				//$s2=explode($end, $s1[1]);
				$s2=explode($end, $cont);
			 	$page=$s2[0];
				
				return $page;
			}
			else
			{
				return null;
			}
			
		}//end of if
		else
		{
			return null;
		}//end of else
		
	}//end of function fetch

	/* THIS FUNCTION WAS IN PARSER.PHP */
	function doHeadings($text)
	{
		for ( $i = 6; $i >= 1; --$i ) 
		{
			$h = str_repeat( '=', $i );
			$text = preg_replace( "/^{$h}(.+){$h}\\s*$/m","<h{$i}>\\1</h{$i}>\\2", $text );
		}
			
		return $text;
	}
	/* THIS FUNCTION WAS IN PARSER.PHP */
	function doAllQuotes($text)
	{
		$outtext = '';
		$lines = explode( "\n", $text );
		foreach ( $lines as $line ) {
			$outtext .= $this->doQuotes( $line ) . "\n";
		}
		$outtext = substr($outtext, 0,-1);

		return $outtext;
	}
	/* THIS FUNCTION WAS IN PARSER.PHP */
	function doQuotes( $text ) 
	{
		$arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
		if ( count( $arr ) == 1 )
			return $text;
		else
		{
			# First, do some preliminary work. This may shift some apostrophes from
			# being mark-up to being text. It also counts the number of occurrences
			# of bold and italics mark-ups.
			$i = 0;
			$numbold = 0;
			$numitalics = 0;
			foreach ( $arr as $r )
			{
				if ( ( $i % 2 ) == 1 )
				{
					# If there are ever four apostrophes, assume the first is supposed to
					# be text, and the remaining three constitute mark-up for bold text.
					if ( strlen( $arr[$i] ) == 4 )
					{
						$arr[$i-1] .= "'";
						$arr[$i] = "'''";
					}
					# If there are more than 5 apostrophes in a row, assume they're all
					# text except for the last 5.
					else if ( strlen( $arr[$i] ) > 5 )
					{
						$arr[$i-1] .= str_repeat( "'", strlen( $arr[$i] ) - 5 );
						$arr[$i] = "'''''";
					}
					# Count the number of occurrences of bold and italics mark-ups.
					# We are not counting sequences of five apostrophes.
					if ( strlen( $arr[$i] ) == 2 )      { $numitalics++;             }
					else if ( strlen( $arr[$i] ) == 3 ) { $numbold++;                }
					else if ( strlen( $arr[$i] ) == 5 ) { $numitalics++; $numbold++; }
				}
				$i++;
			}

			# If there is an odd number of both bold and italics, it is likely
			# that one of the bold ones was meant to be an apostrophe followed
			# by italics. Which one we cannot know for certain, but it is more
			# likely to be one that has a single-letter word before it.
			if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) )
			{
				$i = 0;
				$firstsingleletterword = -1;
				$firstmultiletterword = -1;
				$firstspace = -1;
				foreach ( $arr as $r )
				{
					if ( ( $i % 2 == 1 ) and ( strlen( $r ) == 3 ) )
					{
						$x1 = substr ($arr[$i-1], -1);
						$x2 = substr ($arr[$i-1], -2, 1);
						if ($x1 == ' ') {
							if ($firstspace == -1) $firstspace = $i;
						} else if ($x2 == ' ') {
							if ($firstsingleletterword == -1) $firstsingleletterword = $i;
						} else {
							if ($firstmultiletterword == -1) $firstmultiletterword = $i;
						}
					}
					$i++;
				}

				# If there is a single-letter word, use it!
				if ($firstsingleletterword > -1)
				{
					$arr [ $firstsingleletterword ] = "''";
					$arr [ $firstsingleletterword-1 ] .= "'";
				}
				# If not, but there's a multi-letter word, use that one.
				else if ($firstmultiletterword > -1)
				{
					$arr [ $firstmultiletterword ] = "''";
					$arr [ $firstmultiletterword-1 ] .= "'";
				}
				# ... otherwise use the first one that has neither.
				# (notice that it is possible for all three to be -1 if, for example,
				# there is only one pentuple-apostrophe in the line)
				else if ($firstspace > -1)
				{
					$arr [ $firstspace ] = "''";
					$arr [ $firstspace-1 ] .= "'";
				}
			}

			# Now let's actually convert our apostrophic mush to HTML!
			$output = '';
			$buffer = '';
			$state = '';
			$i = 0;
			foreach ($arr as $r)
			{
				if (($i % 2) == 0)
				{
					if ($state == 'both')
						$buffer .= $r;
					else
						$output .= $r;
				}
				else
				{
					if (strlen ($r) == 2)
					{
						if ($state == 'i')
						{ $output .= '</i>'; $state = ''; }
						else if ($state == 'bi')
						{ $output .= '</i>'; $state = 'b'; }
						else if ($state == 'ib')
						{ $output .= '</b></i><b>'; $state = 'b'; }
						else if ($state == 'both')
						{ $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
						else # $state can be 'b' or ''
						{ $output .= '<i>'; $state .= 'i'; }
					}
					else if (strlen ($r) == 3)
					{
						if ($state == 'b')
						{ $output .= '</b>'; $state = ''; }
						else if ($state == 'bi')
						{ $output .= '</i></b><i>'; $state = 'i'; }
						else if ($state == 'ib')
						{ $output .= '</b>'; $state = 'i'; }
						else if ($state == 'both')
						{ $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
						else # $state can be 'i' or ''
						{ $output .= '<b>'; $state .= 'b'; }
					}
					else if (strlen ($r) == 5)
					{
						if ($state == 'b')
						{ $output .= '</b><i>'; $state = 'i'; }
						else if ($state == 'i')
						{ $output .= '</i><b>'; $state = 'b'; }
						else if ($state == 'bi')
						{ $output .= '</i></b>'; $state = ''; }
						else if ($state == 'ib')
						{ $output .= '</b></i>'; $state = ''; }
						else if ($state == 'both')
						{ $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
						else # ($state == '')
						{ $buffer = ''; $state = 'both'; }
					}
				}
				$i++;
			}
			# Now close all remaining tags.  Notice that the order is important.
			if ($state == 'b' || $state == 'ib')
				$output .= '</b>';
			if ($state == 'i' || $state == 'bi' || $state == 'ib')
				$output .= '</i>';
			if ($state == 'bi')
				$output .= '</b>';
			# There might be lonely ''''', so make sure we have a buffer
			if ($state == 'both' && $buffer)
				$output .= '<b><i>'.$buffer.'</i></b>';
			return $output;
		}
	}//end of function doQuotes
	
	/*$greeting = "Hello World Muhak!";
		
		$model =& $this->getModel();
		$greeting = $model->getGreeting();
		
        $this->assignRef( 'greeting', $greeting );*/
}
?>
